package com.lepu.app.fun.tool.tefypc;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.utils.PopUtil;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

@SuppressLint("InflateParams")
public class TaiErActivity extends BaseFragmentActivity implements 
    OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener {
	
	private TextView mWeekEdit;
	private EditText mBpdEdit;
	private EditText mAcEdit;
	private EditText mFlEdit;
	
	private ImageView remind1;
	private ImageView remind2;
	private ImageView remind3;
	
	private int index=0;

	public String[] array; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tool_taier_activity);

		UtilityBase.setStatusBarRes(this, R.color.color_taidong, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}
	
	@Override
	public void onTopbarRightButtonSelected() {
		startActivity(new Intent(this, TaiErShowActivity.class),true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("胎儿发育评测");
		topbar.setRightText("参考数据");
		topbar.setTopbarBackground(R.color.color_taidong);
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setOnTopbarNewRightButtonListener(this);
		
		array = getResources().getStringArray(R.array.array_taier_week);
		
		mWeekEdit = (TextView) findViewById(R.id.weekEditText);
		mBpdEdit = (EditText) findViewById(R.id.bpdEditText);
		mAcEdit = (EditText) findViewById(R.id.acEditText);
		mFlEdit = (EditText) findViewById(R.id.flEditText);
		
		remind1 = (ImageView) findViewById(R.id.imageRemind1);
		remind2 = (ImageView) findViewById(R.id.imageRemind2);
		remind3 = (ImageView) findViewById(R.id.imageRemind3);
		
		mWeekEdit.setText(array[index]);
		mWeekEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showWeek();
			}
		});
		
	}


	
	@SuppressLint("DefaultLocale")
	public void viewClick(View view) {
		int rid = view.getId();
		switch (rid) {
		case R.id.testButton:

			String week = mWeekEdit.getText().toString().trim();
			String bpd = mBpdEdit.getText().toString().trim();
			String ac = mAcEdit.getText().toString().trim();
			String fl = mFlEdit.getText().toString().trim();

			if (TextUtils.isEmpty(week)) {
				UIHelper.showToast(this, "请完整填写 孕周");
				return;
			}

			if (TextUtils.isEmpty(bpd)) {
				UIHelper.showToast(this, "请完整填写 双顶径");
				return;
			}
			
			if (TextUtils.isEmpty(ac)) {
				UIHelper.showToast(this, "请完整填写 腹围");
				return;
			}
			
			if (TextUtils.isEmpty(fl)) {
				UIHelper.showToast(this, "请完整填写 股骨长");
				return;
			}

			Intent intent = new Intent(TaiErActivity.this, TaiErResultActivity.class);
			intent.putExtra("week", String.valueOf(index+13));
			intent.putExtra("bpd", bpd);
			intent.putExtra("ac", ac);
			intent.putExtra("fl", fl);
			startActivity(intent,true);
			break;

		case R.id.imageRemind1:
			showPop(remind1, "胎头双顶径又称BPD，是指胎儿头部左右两侧之间最宽部位的长度，又称为“头部大横径”。医生常常用它来观察孩子发育的情况，判断能否有头盆不称，以及能否顺利分娩。");
			closeEdit();
		break;
		case R.id.imageRemind2:
			showPop(remind2, "从孕16周开始测量腹围，取立位，以肚脐为准，水平绕腹一周，测得数值即为腹围。");
			closeEdit();
			break;
		case R.id.imageRemind3:
			showPop(remind3, "是医生利用B超(或彩超)给孕妇作孕期检查时，用它来观察胎儿发育的情况常用的指标。股骨长也是孕期计算胎儿体重的重要数据之一，随着孕周的增加，股骨长的数值会逐渐增大，这表明胎儿的身长和体重都在逐渐的增加");
			closeEdit();
			break;
		default:
			break;
		}
	}
	
	
	public void showWeek(){
		
		
		final MyDialog dialog = new MyDialog(this);
		dialog.setSimpleSelectPicker(array, index)
				.setTitle("请选择孕周")
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

								WheelView wv_content = (WheelView) v.findViewById(R.id.simpleSelectPicker);
								index = wv_content.getCurrentItem();

								mWeekEdit.setText(array[index]);

							}
						}).create(null).show();
	}
	
	public void showPop(ImageView image, String content){
		View view = LayoutInflater.from(this).inflate(R.layout.tool_tip_layout, null);
		TextView hospitalNotifyTextView = (TextView) view.findViewById(R.id.textView);
		hospitalNotifyTextView.setText(content);
		PopUtil.getInstance().showPopWindow(this, image,view, Gravity.NO_GRAVITY, null, true);
	}
	
	public void closeEdit(){
		  View view = getWindow().peekDecorView();
	        if (view != null) {
	            InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
	            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
	        }
	}

}
